package com.infusionesuci2;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.infusionesuci2.R;
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.PushService;

import android.net.ConnectivityManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class Notificacion extends Activity{

	public WebView pushRemote; 
	public ProgressBar progressBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notificacion);
		
		// Analytics
		GoogleAnalytics.getInstance(this).getTracker("UA-45986553-3");
		// -------------
		
		this.pushRemote = (WebView) findViewById(R.id.remotePush);
		this.progressBar = (ProgressBar)findViewById(R.id.progs);
		
		this.pushRemote.getSettings().setJavaScriptEnabled(true);
		//this.pushRemote.getSettings().setBuiltInZoomControls(true);
		this.pushRemote.getSettings().setPluginsEnabled(true); 
		
		Intent intent = getIntent();
		//String mensaje = intent.getStringExtra("message");
		//String titulo = intent.getStringExtra("title");
		String URL = intent.getStringExtra("url");
		//TextView titulos = (TextView) findViewById(R.id.titulo);
		//TextView descripcion = (TextView) findViewById(R.id.descripcion);
		//titulos.setText(titulo);
		//descripcion.setText(mensaje);
		
		
		
		if(this.hayInternet()){
			
			try {
				
				this.pushRemote.loadUrl(URL);
				
				this.pushRemote.setWebViewClient(new WebViewClient()
				{
				               // evita que los enlaces se abran fuera nuestra app en el navegador de android
				   @Override
				   public boolean shouldOverrideUrlLoading(WebView view, String url)
				   {
				       return false;
				   }   
				    
				});

				this.pushRemote.setWebChromeClient(new WebChromeClient()
				{
				   @Override
				   public void onProgressChanged(WebView view, int progress) 
				   {               
				       progressBar.setProgress(0);
				       progressBar.setVisibility(View.VISIBLE);
				       progressBar.setProgress(progress);

				       progressBar.incrementProgressBy(progress*10);

				       if(progress == 100)
				       {
				           progressBar.setVisibility(View.GONE);
				       }
				   }
				});   
				
			} catch (Exception e) {
				
				String mapaNoDisponible ="<html><head></head><body>" +
			            "<b>"+this.getString(R.string.FaltaInternet)+"</b>" +
			            "</body><html>";
				this.pushRemote.loadDataWithBaseURL("file:///android_asset/", mapaNoDisponible, "text/html", "utf-8", null);
				progressBar.setProgress(100);
				
			}
			
		}
		else{ 
			String mapaNoDisponible ="<html><head></head><body>" +
		            "<b>"+this.getString(R.string.FaltaInternet)+"</b>" +
		            "</body><html>";
			this.pushRemote.loadDataWithBaseURL("file:///android_asset/", mapaNoDisponible, "text/html", "utf-8", null);
			progressBar.setProgress(100);
		}
		
		// Initialize the Parse SDK.
		Parse.initialize(this, "aROtMg7797G0Ya3I4oQshaksY5pRRQCo9kSot90k","kORY5LADI60NClqP98wzwWT4QHCZBki4MLW5ESyn");		
		// Specify a Activity to handle all pushes by default.
		PushService.setDefaultPushCallback(this, Notificacion.class);
		
		// Save the current installation.
		ParseInstallation.getCurrentInstallation().saveInBackground();
	}

	private boolean hayInternet() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;
		} else {
			return false;
		}
	}
	
	public void home(View view) {
		this.finish();
	}

	 @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance(this).activityStart(this); 
	  }
	 
	 @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance(this).activityStop(this);
	  }
}
